package com.coscoshipping.rams.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coscoshipping.rams.demo.entity.TicketSalesStatistics;
import com.coscoshipping.rams.demo.mapper.TicketSalesStatisticsMapper;
import com.coscoshipping.rams.demo.pojo.vo.TicketSalesVO;
import com.coscoshipping.rams.demo.service.ITicketSalesStatisticsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoyin.yang
 * @since 2025-04-19
 */
@Service
public class TicketSalesStatisticsServiceImpl extends ServiceImpl<TicketSalesStatisticsMapper, TicketSalesStatistics> implements ITicketSalesStatisticsService {

    /**
     * 保存或更新数据到数据库
     */
    @Override
    @DS("slave_1")
    @Transactional
    public void saveOrUpdateData(List<TicketSalesVO> voList) {
        if (voList == null || voList.isEmpty()) {
            return;
        }

        // 转换为实体类列表
        List<TicketSalesStatistics> entityList = voList.stream()
                .map(vo -> {
                    TicketSalesStatistics entity = new TicketSalesStatistics();
                    entity.setSalesDate(vo.getSalesDate());
                    entity.setTicketTypeCategory(vo.getTicketTypeCategory());
                    entity.setTicketType(vo.getTicketType());
                    entity.setCustomerType(vo.getCustomerType());
                    entity.setTransactionType(vo.getTransactionType());
                    entity.setTicketVariety(vo.getTicketVariety());
                    entity.setUnitPrice(vo.getUnitPrice());
                    entity.setQuantity(vo.getQuantity());
                    entity.setAmount(vo.getAmount());
                    entity.setAfterTaxAmount(vo.getAfterTaxAmount());
                    return entity;
                })
                .collect(Collectors.toList());

        // 批量插入或更新
        entityList.forEach(entity -> {
            // 判断是否存在相同记录
            TicketSalesStatistics existingRecord = this.lambdaQuery()
                    .eq(TicketSalesStatistics::getSalesDate, entity.getSalesDate())
                    .eq(TicketSalesStatistics::getTicketTypeCategory, entity.getTicketTypeCategory())
                    .eq(StrUtil.isNotBlank(entity.getTicketType()),TicketSalesStatistics::getTicketType, entity.getTicketType())
                    .eq(StrUtil.isNotBlank(entity.getCustomerType()), TicketSalesStatistics::getCustomerType, entity.getCustomerType())
                    .eq(StrUtil.isNotBlank(entity.getTransactionType()),TicketSalesStatistics::getTransactionType, entity.getTransactionType())
                    .eq(StrUtil.isNotBlank(entity.getTicketVariety()),TicketSalesStatistics::getTicketVariety, entity.getTicketVariety())
                    .eq(TicketSalesStatistics::getUnitPrice, entity.getUnitPrice()) // 新增对单价的判断
                    .one();

            if (existingRecord == null) {
                // 如果不存在，则插入
                this.save(entity);
            } else {
                // 如果存在，则忽略
                System.out.println("忽略重复数据: " + entity);
            }
        });
    }
}